package com.ruoyi.coal.modules.coalpile.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.Query;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalYardInfoVo;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalYardVo;
import com.ruoyi.coal.modules.coalpile.mapper.CoalPileInfoDao;
import com.ruoyi.coal.modules.coalpile.mapper.CoalYardInfoDao;
import com.ruoyi.coal.modules.coalpile.service.CoalYardInfoService;
import com.ruoyi.coal.modules.coalpile.service.PointService;
import com.ruoyi.coal.modules.minio.MinioProperties;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("coalYardInfoService")
//@DataSource(value = DataSourceType.SLAVE)
public class CoalYardInfoServiceImpl extends ServiceImpl<CoalYardInfoDao, CoalYardInfoEntity> implements CoalYardInfoService {
    @Autowired
    private CoalYardInfoDao coalYardInfoDao;

    @Autowired
    private MinioProperties minioProperties;

    @Autowired
    private CoalPileInfoDao coalPileInfoDao;

    @Autowired
    private PointService pointService;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CoalYardInfoEntity> wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty((String)params.get("key")))
            ((QueryWrapper)((QueryWrapper)wrapper.eq("id", params.get("key"))).or()).like("name", params.get("key"));
        IPage<CoalYardInfoEntity> page = page((new Query())
                .getPage(params), (Wrapper)wrapper);
        return new PageUtils(page);
    }

    public List<CoalYardVo> getCoalIdList() {
        List<CoalYardInfoEntity> entities = list();
        List<CoalYardVo> list = (List<CoalYardVo>)entities.stream().map(entity -> {
            CoalYardVo coalYardVo = new CoalYardVo();
            coalYardVo.setId(entity.getId());
            coalYardVo.setName(entity.getName());
            return coalYardVo;
        }).collect(Collectors.toList());
        return list;
    }

    public List<CoalYardInfoVo> getAll() {
        List<CoalYardInfoEntity> list = list();
        List<CoalYardInfoVo> voList = (List<CoalYardInfoVo>)list.stream().map(item -> {
            CoalYardInfoVo coalYardInfoVo = new CoalYardInfoVo();
            BeanUtils.copyProperties(item, coalYardInfoVo);
            return coalYardInfoVo;
        }).collect(Collectors.toList());
        return voList;
    }

    public CoalYardInfoEntity queryImportCloudInfo(String name) {
        CoalYardInfoEntity name1 = (CoalYardInfoEntity)this.coalYardInfoDao.selectOne((Wrapper)(new QueryWrapper()).eq("name", name));
        return name1;
    }

    public File getALLFile(String url) {
        String file = url;
        File Folder = new File(file);
        List<String> fileNames = new ArrayList<>();
        findFileList(new File(file), fileNames);
        File destFile = new File(Folder, fileNames.get(0));
        for (int i = 1; i < fileNames.size(); i++) {
            File sourceFile = new File(Folder, fileNames.get(i));
            try(BufferedReader reader = new BufferedReader(new FileReader(sourceFile));
                BufferedWriter writer = new BufferedWriter(new FileWriter(destFile, true))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    writer.write(line);
                    writer.newLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new File(Folder, fileNames.get(0));
    }

    public void removeFile(String url) {
        String deleteFile = url;
        File delfile = new File(deleteFile);
        File[] files = delfile.listFiles();
        files[0].delete();
    }

    public void findFileList(File dir, List<String> fileNames) {
        if (!dir.exists() || !dir.isDirectory())
            return;
        String[] files = dir.list();
        for (int i = 0; i < files.length; i++) {
            File file = new File(dir, files[i]);
            if (file.isFile()) {
                fileNames.add(file.getName());
            } else {
                findFileList(file, fileNames);
            }
            if (file.length() == 0L)
                return;
        }
    }

    public int filecount() {
        int filenum = 0;
        String path = this.minioProperties.getFilename();
        File d = new File(path);
        File[] list = d.listFiles();
        for (int i = 0; i < list.length; i++) {
            if (list[i].isFile())
                filenum++;
        }
        return filenum;
    }

    public List<String> getCoalYardNameByIds(List<Integer> coalYardIds) {
        List<String> coalYardNames = this.coalYardInfoDao.getCoalYardNameByIds(coalYardIds);
        return coalYardNames;
    }
}
